import {combineReducers } from 'redux'
import { routerReducer } from 'react-router-redux'
import {
    START_SEARCH,
    RECEIVE_SEARCH,
    RECEIVE_BOOKINTRO,
    RECEIVE_COMMENT,
    RECEIVE_BOOK_CONTENT,
    RECEIVE_BOOK_CHAPTER,
} from '../actions/actions'

const searchKeyword = (state= {
    searchKeyword,
    isShow:false,
},action) => {
    switch(action.type){
        case START_SEARCH:
            return {
                ...state,
                searchKeyword:action.searchKeyowrd,
                isShow:action.isShow
            }
        default:
            return state
    }
}

const searchResult = (state = {
    searchKeyowrd:'',
    list:[],
    receiveAt:''}, action) => {
    switch(action.type){
        case RECEIVE_SEARCH:
            return {
                ...state,
                searchKeyowrd:action.searchKeyowrd,
                list:action.list,
                receiveAt:action.receiveAt
            }
        default:
            return state
    }
}
const bookIntro = (state= {
    id:'',
    isShow:true,
    detail:'',
    receiveAt:''
}, action) =>{
    switch(action.type){
        case RECEIVE_BOOKINTRO:
            return {
                ...state,
                id:action.id,
                isShow:action.isShow,
                detail:action.detail,
                receiveAt:action.receiveAt
            }
        default:
            return state
    }
}
const bookComment = (state= {
    id:'',
    comments:'',
    receiveAt:''
}, action) =>{
    switch(action.type){
        case RECEIVE_COMMENT:
            return {
                ...state,
                id:action.id,
                comments:action.comments,
                receiveAt:action.receiveAt
            }
        default:
            return state
    }
}
const bookChapterList = (state={
    id:'',
    isShow:true,
    list:[],
    receiveAt:''
},action)=>{
    switch(action.type){
        case RECEIVE_BOOK_CHAPTER:
            return {
                ...state,
                id:action.id,
                isShow:action.isShow,
                list:action.list,
                receiveAt:action.receiveAt
            }
        default :
            return state
    }
}
const bookContent = (state={
    chapter:[],
    isShow:false
},action)=>{
    switch(action.type){
        case RECEIVE_BOOK_CONTENT:
            return {
                ...state,
                chapter:action.chapter,
                isShow:action.isShow
            }
        default:
            return state
    }
}

const rootReducer = combineReducers ({
    searchKeyword,
    searchResult,
    bookIntro,
    bookComment,
    bookContent,
    bookChapterList,
    router: routerReducer
})

export default rootReducer